package cn.com.company.main.client.ui;

import cn.com.company.main.client.DesktopApp;
import cn.com.company.main.client.entity.WebbomUserinfo;
import cn.com.company.main.client.service.GreetingService;
import cn.com.company.main.client.service.GreetingServiceAsync;

import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.event.BaseEvent;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.event.MessageBoxEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.widget.Dialog;
import com.extjs.gxt.ui.client.widget.Info;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.FieldSet;
import com.extjs.gxt.ui.client.widget.form.FormPanel;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.layout.FlowLayout;
import com.extjs.gxt.ui.client.widget.layout.FormData;
import com.extjs.gxt.ui.client.widget.layout.FormLayout;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;

public class LoginDialog extends Dialog {
	private FormData formData = new FormData("-20");
	private FormPanel form;
	private FieldSet fieldSet;
	private TextField<String> loginName;
	private TextField<String> password;
	private Button okButton;
	private Button cancelButton;
	private static LoginDialog instance;

	public static LoginDialog getInstance() {
		if (instance == null) {
			instance = new LoginDialog();
		}
		return instance;
	}

	public LoginDialog() {
		setBodyBorder(false);
		setLayout(new FlowLayout());
		setClosable(false);
		this.add(createLoginForm());
		this.getButtonById(Dialog.OK).setVisible(false);
	}

	public void show() {
		setHeading("上海恒为信息科技有限公司Web BOM");
		setWidth(350);
		setHeight(200);
		setDraggable(false);
		setResizable(false);
		setModal(true);
		setMonitorWindowResize(true);
		clearTextField();
		super.show();
		center();
	}

	public FormPanel createLoginForm() {
		form = new FormPanel();
		form.setFrame(true);
		form.setHeaderVisible(false);
		form.setWidth(335);
		form.setHeight(160);
		form.setLayout(new FlowLayout());

		fieldSet = new FieldSet();
		fieldSet.setHeading("用户登录");
		FormLayout layout = new FormLayout();
		layout.setLabelWidth(75);
		fieldSet.setLayout(layout);

		loginName = new TextField<String>();
		loginName.setFieldLabel("用户名");
		fieldSet.add(loginName, formData);

		password = new TextField<String>();
		password.setFieldLabel("密&nbsp;&nbsp;&nbsp;&nbsp;码");
		password.setPassword(true);
		fieldSet.add(password, formData);

		form.add(fieldSet);

		form.setButtonAlign(HorizontalAlignment.RIGHT);
		
		okButton = new Button("确&nbsp;&nbsp;定");
		okButton.addSelectionListener(new SelectionListener<ButtonEvent>() {

			@Override
			public void componentSelected(ButtonEvent ce) {
				String uname = loginName.getValue();
				String pwd = password.getValue();
				if (uname == null || "".equals(uname.trim())) {
					MessageBox.alert("提示", "请输入用户名！", new Listener<MessageBoxEvent>() {
						@Override
						public void handleEvent(MessageBoxEvent be) {
							loginName.focus();
						}
					});
					return;
				}
				if (pwd == null || "".equals(pwd.trim())) {
					MessageBox.alert("提示", "请输入密码！", new Listener<MessageBoxEvent>() {
						
						@Override
						public void handleEvent(MessageBoxEvent be) {
							password.focus();
						}
					});
					return;
				}
				GreetingServiceAsync service = GWT.create(GreetingService.class);
				service.checkLoginUser(uname, pwd, new AsyncCallback<WebbomUserinfo>() {
					@Override
					public void onSuccess(WebbomUserinfo entity) {
						if(entity == null){
							MessageBox.alert("提示", "用户名或密码错误，请确认！", null);
							clearTextField();
						}
						else{
							//Info.display("提示", "登录成功！");
							hide();
							DesktopApp app = new DesktopApp();
							app.desktopInit(entity);
						}
					}
					
					@Override
					public void onFailure(Throwable caught) {
						MessageBox.alert("提示", "登录失败！<br>"+caught.getLocalizedMessage(), null);
					}
				});
			}
		});
		cancelButton = new Button("重&nbsp;&nbsp;置");
		cancelButton.addSelectionListener(new SelectionListener<ButtonEvent>() {

			@Override
			public void componentSelected(ButtonEvent ce) {
				clearTextField();
			}
		});

		form.addButton(okButton);
		form.addButton(cancelButton);
		
		form.addListener(Events.KeyUp, new Listener<BaseEvent>() {

			@Override
			public void handleEvent(BaseEvent be) {
				Info.display("提示", "Hello");
				
			}
			
		});
		return form;
	}
	
	public void clearTextField(){
		loginName.setValue("");
		password.setValue("");
	}
	
}
